 #include "cv.h" 
 #include "highgui.h"
 #include "ImageProcessor.h"
 #include "FrameGrammar.h"
 #include "RtMidi.h"
 #include <stdio.h> 

 // A Simple Camera Capture Framework 
 int main() {
   CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
   if ( !capture ) {
     fprintf( stderr, "ERROR: capture is NULL \n" );
     getchar();
     return -1;
   }

   int calibrateStep = 0;
   // Create a window in which the captured images will be presented
   cvNamedWindow( "Rockin'spiel", CV_WINDOW_AUTOSIZE );

   // Get one frame
   IplImage* frame = cvQueryFrame( capture );
   if ( !frame ) {
     fprintf( stderr, "ERROR: frame is null...\n" );
     getchar();
	 return 0;
     }

   //cvSmooth(frame, frame, CV_GAUSSIAN, 15, 15);

   ImageProcessor ip(frame);
   FrameGrammar fg(new MIDIProcessor);
   int key;



   // Show the image captured from the camera in the window and repeat
   while ( 1 ) {
     // Get one frame
     IplImage* frame = cvQueryFrame( capture );
     if ( !frame ) {
       fprintf( stderr, "ERROR: frame is null...\n" );
       getchar();
       break;
     }

	 //ip.Calibrate(frame);

	 // first process blobs
	 ip.processImage(frame);

	 // then process frame actions
	 // but only if calibration is complete
	 if(calibrateStep > 5)
		fg.processFrame(&ip);


     cvShowImage( "Rockin'spiel", frame );

     // Do not release the frame!
     //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
     //remove higher bits using AND operator
     //if ( (cvWaitKey(10) & 255) == 27 ) break;
	 key = cvWaitKey(10);

	 if((key & 255) == 27)	// ESC - stop
		 break;
	 else if((key & 255) == 13 && calibrateStep < 6) // ENTER - advance calibration
	 {
		 ip.calibrateStep(calibrateStep);
		 calibrateStep++;
	 }
   }
   // Release the capture device housekeeping
   cvReleaseCapture( &capture );
   cvDestroyWindow( "Rockin'spiel" );
   return 0;
 }